package com.idlecode.manage;

import cn.hutool.core.util.StrUtil;
import com.idlecode.bean.DataSourceEntity;
import com.idlecode.panel.DataSourcesPanel;
import com.idlecode.service.DataSourceService;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.UUID;

/**
 * @program: IdleCode
 * @description
 * @author: rongjuwei
 * @create: 2024-02-06 16:00
 **/
public class DataSourceFormManage extends DialogWrapper {
    private static final String[] databaseTypes = new String[]{"MySQL", "MariaDB", "Oracle", "PostgreSQL", "SQLServer", "DM", "Clickhouse"};
    private JTextField connectionNameField;
    private ComboBox<String> databaseTypeComboBox;
    private JTextField databaseUrlField;
    private JTextField userNameField;
    private JTextField passwordField;
    private Integer indexOf;
    private String dsId;
    private String connectionName;
    private String databaseType;
    private String databaseUrl;
    private String userName;
    private String password;
    private DataSourcesPanel dataSourcesPanel;

    public DataSourceFormManage(Integer indexOf, DataSourceEntity dataSourceEntity, DataSourcesPanel dataSourcesPanel) {
        super(true);
        this.indexOf = indexOf;
        this.dsId = dataSourceEntity.getDsId();
        this.connectionName = dataSourceEntity.getConnName();
        this.databaseType = dataSourceEntity.getDbType();
        this.databaseUrl = dataSourceEntity.getConnUrl();
        this.userName = dataSourceEntity.getUsername();
        this.password = dataSourceEntity.getPassword();
        this.dataSourcesPanel = dataSourcesPanel;
        setTitle("数据源设置");
        init();
    }

    public DataSourceFormManage(DataSourcesPanel dataSourcesPanel) {
        super(true); // 设置为模态对话框
        setTitle("数据源设置");
        this.dataSourcesPanel = dataSourcesPanel;
        init(); // 初始化对话框
    }

    private static JPanel createPanelWithFixedSize(String labelText) {
        JLabel label = new JLabel(labelText);
        label.setPreferredSize(new Dimension(70, label.getPreferredSize().height));
        JPanel panel = new JPanel(new FlowLayout());
        panel.setPreferredSize(new Dimension(450, label.getPreferredSize().height));
        panel.add(label);
        return panel;
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        JPanel panel = new JPanel(new GridLayout(5, 2));
        panel.setPreferredSize(new Dimension(450, 200));

        JPanel connectionNamePane = createPanelWithFixedSize("连接名:");
        connectionNameField = new JTextField(connectionName);
        connectionNameField.setColumns(25);
        connectionNamePane.add(connectionNameField);

        JPanel databaseTypePane = createPanelWithFixedSize("数据源类型:");
        databaseTypeComboBox = new ComboBox<>(databaseTypes, 280);
        for (int i = 0; i < databaseTypes.length; i++) {
            if (databaseTypes[i].equals(databaseType)) {
                databaseTypeComboBox.setSelectedIndex(i);
            }
        }
        databaseTypePane.add(databaseTypeComboBox);

        JPanel databaseUrlPane = createPanelWithFixedSize("数据源URL:");
        databaseUrlField = new JTextField(databaseUrl);
        databaseUrlField.setColumns(25);
        databaseUrlPane.add(databaseUrlField);

        JPanel userNamePane = createPanelWithFixedSize("用户名:");
        userNameField = new JTextField(userName);
        userNameField.setColumns(25);
        userNamePane.add(userNameField);

        JPanel passwordPane = createPanelWithFixedSize("密码:");
        passwordField = new JTextField(password);
        passwordField.setColumns(25);
        passwordPane.add(passwordField);

        panel.add(connectionNamePane);
        panel.add(databaseTypePane);
        panel.add(databaseUrlPane);
        panel.add(userNamePane);
        panel.add(passwordPane);

        return panel;
    }

    @Override
    protected void doOKAction() {
        saveConnectionSettings();
    }

    private void saveConnectionSettings() {
        connectionName = connectionNameField.getText();
        if (StringUtil.isEmpty(connectionName)) {
            Messages.showMessageDialog("连接名不能为空！", "警告", Messages.getWarningIcon());
            return;
        }
        databaseType = databaseTypeComboBox.getSelectedItem().toString();
        if (StringUtil.isEmpty(databaseType)) {
            Messages.showMessageDialog("数据源类型不能为空！", "警告", Messages.getWarningIcon());
            return;
        }
        databaseUrl = databaseUrlField.getText();
        if (StringUtil.isEmpty(databaseUrl)) {
            Messages.showMessageDialog("数据源URL不能为空！", "警告", Messages.getWarningIcon());
            return;
        }
        userName = userNameField.getText();
        if (StringUtil.isEmpty(userName)) {
            Messages.showMessageDialog("用户名不能为空！", "警告", Messages.getWarningIcon());
            return;
        }
        password = passwordField.getText();
        if (StringUtil.isEmpty(password)) {
            Messages.showMessageDialog("密码不能为空！", "警告", Messages.getWarningIcon());
            return;
        }

        List<DataSourceEntity> dataSourcesRows = DataSourceService.getInstance().getDataSources();
        DataSourceEntity dataSourceEntity = new DataSourceEntity(StrUtil.isBlank(dsId) ? UUID.randomUUID().toString(): dsId,
                databaseType, connectionName, databaseUrl, userName, password);
        if (indexOf == null) {
            dataSourcesRows.add(dataSourceEntity);
        } else {
            dataSourcesRows.set(indexOf, dataSourceEntity);
        }
        DataSourceService.getInstance().setDataSources(dataSourcesRows);
        dataSourcesPanel.queryBtnAction();
        super.doOKAction();
    }
}
